/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package com.airlogger.log.view
{
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	
	import mx.controls.HTML;
	
	import com.airlogger.event.AirLoggerLogEvent;
	import com.airlogger.log.AirLoggerLogPlugin;
	import com.airlogger.log.event.LogDataEvent;
	import com.airlogger.log.model.AirLoggerModelList;
	import com.airlogger.log.model.LogData;
	import com.airlogger.log.model.LogModel;
	import com.airlogger.log.model.UIModel;
	import com.airlogger.log.view.format.FormatUtils;
	import com.airlogger.proxy.AirLoggerProxyEventList;
	import com.airlogger.utils.LogLevelUtils;
	import com.bourre.collection.Iterator;
	import com.bourre.collection.Stack;
	import com.bourre.events.StringEvent;
	import com.bourre.log.LogLevel;
	import com.bourre.log.PixlibDebug;
	import com.bourre.plugin.Plugin;
	import com.bourre.view.AbstractView;
	import com.bourre.events.BasicEvent;
	import com.airlogger.event.SWFEvent;		

	public class LogView extends AbstractView
	{	
		public var htmlContent : HTML;

		public function LogView( owner : Plugin = null, name : String = null, mc : DisplayObjectContainer = null )
		{
			super(owner, name, mc);
			
			htmlContent = view.getChildByName( "htmlContent" ) as HTML;
			htmlContent.addEventListener( Event.COMPLETE, complete, false, 1 );
			htmlContent.location = "template/bridge.html";
		}
		public function complete (event : Event) : void
		{
			htmlContent.domWindow.host = this;
			htmlContent.domWindow.init();
		} 
		public function onSWFAdded ( event : StringEvent ) : void
		{
			htmlContent.domWindow.refreshTabs( (event.target as UIModel ).getSWFs() );
		}		 
		public function onSWFRemoved ( event : StringEvent ) : void
		{	
			htmlContent.domWindow.refreshTabs( (event.target as UIModel ).getSWFs() );	
		}		 
		public function onFocusChanged( event : StringEvent ) : void
		{
			var model : UIModel = getOwner().getModelLocator().getModel( AirLoggerModelList.UI ) as UIModel;
			var logModel : LogModel = getOwner().getModelLocator().getModel( model.getFocus() ) as LogModel;
			
			var id : String = logModel.getLogFilter().getName();
		
			//htmlContent.domWindow.selectFilter( id.toLowerCase() );
			htmlContent.domWindow.selectSWF( model.getFocus() );
		}
		public function onLogAdded ( event : LogDataEvent ) : void
		{
			//htmlContent.domWindow.logAdded( event.data );

			//PixlibDebug.DEBUG( "before format : " + event.data );
			try
			{	
				var s : String = FormatUtils.format( event.data );
			}
			catch( e : Error )
			{
				//PixlibDebug.ERROR( "error : " + e.getStackTrace() );
			}
			
			//PixlibDebug.DEBUG( "after format : " + s );
			htmlContent.domWindow.add( s );
		}		
		
		public function onTabChanged ( event : BasicEvent ) : void
		{
			var model : UIModel = event.target as UIModel;
			
			htmlContent.domWindow.refreshTabs( model.getSWFs() );	
			htmlContent.domWindow.selectSWF( model.getFocus() );
		}
		
		public function onRefresh ( event : StringEvent ) : void
		{
			if( getOwner().getModelLocator().isRegistered( event.getString() ) )
			{
				var model : LogModel = getOwner().getModelLocator().getModel( event.getString() ) as LogModel;
				
				var stack : Stack = model.getFilteredLogs();
				
				print( stack );
			}
			else
			{
				print( new Stack() );
			}
		}		
		
		protected function print ( stack : Stack ) : void
		{
			/*
			//htmlContent.domWindow.refreshLogs( stack.toArray() );
			
			var i : Iterator = stack.iterator();
			var content : String = "";

			while( i.hasNext() )
			{
				var data : LogData = i.next() as LogData;
				
				content += FormatUtils.format(data);
			}
			
			htmlContent.domWindow.insert( content );
			*/
		}
		
		public function sendFilter( filter : String ) : void
		{
			var level : LogLevel = LogLevelUtils.getLevelFromName( filter );
			
			( getOwner() as AirLoggerLogPlugin).fireOnChangeFilterEvent( level );
		}
		
		public function sendFilterMode ( b : Boolean ) : void
		{
			var uimodel : UIModel = getOwner().getModelLocator().getModel( AirLoggerModelList.UI ) as UIModel;
			var m : LogModel = getOwner().getModelLocator().getModel( uimodel.getFocus() ) as LogModel;
			
			m.setFilterMode ( b ? 0 : 1 );
			uimodel.fireOnRefreshEvent();
		}

		public function sendClear() : void
		{
			(getOwner( ) as AirLoggerLogPlugin).fireOnClearLogsEvent();
		}
		
		public function sendSearch() : void
		{
			PixlibDebug.DEBUG( "UIView.sendSearch()" );
		}
		
		public function sendSelection( id : String ) : void
		{
			var evt : AirLoggerLogEvent = new AirLoggerLogEvent ( AirLoggerProxyEventList.onFocusEVENT, this, id );
			getOwner().firePrivateEvent( evt );
		}
		
		public function closeTab ( id : String ):void
		{
			//PixlibDebug.DEBUG( "in close tab " + id );
			
			var evt : SWFEvent = new SWFEvent( AirLoggerLogPlugin.onSWFDisconnectEVENT, this, id );
			getOwner().firePrivateEvent( evt );
		}
		
	}
}